package cn.danger.com.fragment.calender;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.danger.com.activity.MainActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BaseFragment;
import cn.danger.com.model.entity.Schedule;
import cn.danger.com.presenter.SchedulePresenter;
import cn.danger.com.presenter.SchedulePresenterImpl;
import cn.danger.com.util.Constants;
import cn.danger.com.util.DateUtil;
import cn.danger.com.view.CommonView;
import cn.danger.com.view.adapter.CalendarScheduleAdapter;
import cn.danger.com.view.adapter.ScheduleAdapter;
import cn.danger.com.widget.MonthDateView;

/**
 * Created by 俊 on 2016/12/26.
 */
public class CalendarFragment extends BaseFragment implements View.OnClickListener, CommonView {

    private View view;
    private MainActivity activity;
    private ImageView left_imageView;
    private ImageView right_imageView;
    private TextView date_textView;
    private TextView week_textView;
    private MonthDateView monthDateView;
    private List<Integer> tasks = new ArrayList<>();
    private ListView listView;
    private ProgressDialog dialog;
    private SchedulePresenter presenter;
    private CalendarScheduleAdapter adapter;
    private List<Schedule> schedules;
    private String day = "";
    private MyReceiver receiver;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_calendar, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (MainActivity) getActivity();
        left_imageView = (ImageView) view.findViewById(R.id.left_imageView);
        right_imageView = (ImageView) view.findViewById(R.id.right_imageView);
        date_textView = (TextView) view.findViewById(R.id.date_textView);
        week_textView = (TextView) view.findViewById(R.id.week_textView);
        monthDateView = (MonthDateView) view.findViewById(R.id.monthDateView);
        monthDateView.setTextView(date_textView, week_textView);
        monthDateView.setmCircleRadius(8);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        listView = (ListView) view.findViewById(R.id.listView);
        Time time = new Time("GMT+8");
        time.setToNow();
        day = DateUtil.getDate(time.year + "", (time.month + 1) + "", time.monthDay + "");

        presenter = new SchedulePresenterImpl(activity, this);
        tasks = presenter.getMonthSchedules(time.year + "", DateUtil.getMonth((time.month + 1) + ""));
        monthDateView.setDaysHasThingList(tasks);

        schedules = presenter.getTodaySchedules();
        adapter = new CalendarScheduleAdapter(activity, schedules, presenter);
        listView.setAdapter(adapter);

        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter("update_schedule_list");
        activity.registerReceiver(receiver, filter);
    }

    @Override
    public void bindEvent() {
        left_imageView.setOnClickListener(this);
        right_imageView.setOnClickListener(this);
        monthDateView.setDateClick(new MonthDateView.DateClick() {
            @Override
            public void onClickOnDate() {
                day = DateUtil.getDate(monthDateView.getmSelYear() + "", (monthDateView.getmSelMonth() + 1) + "", monthDateView.getmSelDay() + "");
                schedules = presenter.getDaySchedules(day);
                adapter = new CalendarScheduleAdapter(activity, schedules, presenter);
                listView.setAdapter(adapter);
            }
        });
        activity.getTodayImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Time time = new Time("GMT+8");
                time.setToNow();
                day = DateUtil.getDate(time.year + "", (time.month + 1) + "", time.monthDay + "");
                tasks = presenter.getMonthSchedules(time.year + "", DateUtil.getMonth((time.month + 1) + ""));
                monthDateView.setDaysHasThingList(tasks);
                monthDateView.setTodayToView();
                schedules = presenter.getDaySchedules(day);
                adapter = new CalendarScheduleAdapter(activity, schedules, presenter);
                listView.setAdapter(adapter);
            }
        });
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
        if (message.equals(Constants.SUCCESS_TO_DELETE_LOCAL) || message.equals(Constants.SUCCESS_TO_DELETE_SCHEDULE)) {
            activity.handler.sendEmptyMessage(1);
        }
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            update();
        }
    }

    public void update() {
        listView.setAdapter(null);
        tasks = presenter.getMonthSchedules(day.split("-")[0], day.split("-")[1]);
        monthDateView.setDaysHasThingList(tasks);
        schedules = presenter.getDaySchedules(day);
        adapter = new CalendarScheduleAdapter(activity, schedules, presenter);
        listView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_imageView:
                tasks.clear();
                String previous_year_and_month = DateUtil.getPreviousYearAndMonth(monthDateView.getmSelYear() + "",
                        String.valueOf(monthDateView.getmSelMonth() + 1));
                String previous_year = previous_year_and_month.split("-")[0];
                String previous_month = previous_year_and_month.split("-")[1];
                tasks = presenter.getMonthSchedules(previous_year, previous_month);
                monthDateView.setDaysHasThingList(tasks);
                monthDateView.onLeftClick();
                day = DateUtil.getDate(previous_year, previous_month, monthDateView.getmSelDay() + "");
                update();
                break;
            case R.id.right_imageView:
                tasks.clear();
                String next_year_and_month = DateUtil.getNextYearAndMonth(monthDateView.getmSelYear() + "",
                        String.valueOf(monthDateView.getmSelMonth() + 1));
                String next_year = next_year_and_month.split("-")[0];
                String next_month = next_year_and_month.split("-")[1];
                tasks = presenter.getMonthSchedules(next_year_and_month.split("-")[0], next_year_and_month.split("-")[1]);
                monthDateView.setDaysHasThingList(tasks);
                monthDateView.onRightClick();
                day = DateUtil.getDate(next_year, next_month, monthDateView.getmSelDay() + "");
                update();
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (receiver != null) {
            activity.unregisterReceiver(receiver);
        }
    }
}